package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierServcie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @author Mr.zhao
 * @version 1.0
 * @date 2021/12/31 10:49
 */
@Service
public class SupplierServiceImpl implements SupplierServcie {

    @Autowired
    private SupplierDao supplierDao;

    @Override
    public Map<String, Object> getSupplierList(Integer page,
                                               Integer rows,
                                               String supplierName) {
        HashMap<String, Object> result = new HashMap<>();
        List<Supplier> supplier = supplierDao.getSupplierList((page-1)*rows , rows ,supplierName);
        result.put("rows",supplier);
        result.put("total",supplier.size());


        return result;


    }

    @Override
    public void saveOrUpdateSupplier(Supplier supplier) {
        Integer supplierId = supplier.getSupplierId();
        if (supplierId == null ) {
            //新增
            supplierDao.save(supplier);

        } else {
            //修改
            supplierDao.update(supplier);
        }
    }

    @Override
    public void delete(String ids) {
        StringBuilder sb = new StringBuilder();
        String strIds = sb.append("(").append(ids).append(")").toString();

        supplierDao.delete(strIds);
    }
}
